<?

//	**********************************************
//	SbSE SQL Constructor class v0.9.1 / 2007.01.25
//	**********************************************

class SQL {
	var $kind;
	var $res;
	var $pre;
	var $insert_id;
	var $num_rows;
	var $fet;
	
	function SQL( $k = 'SELECT' ) {
		$this->pre = '';
		$this->insert_id = 0;
		$this->num_rows = 0;
		$this->fet = 0;

		if (strlen($k) > 6) {
			$this->pre = $k;
			$this->kind = explode(' ', $k, 2);
			$this->kind = strtoupper($this->kind[0]);
		}	
			else $this->kind = $k;
	}

	function run() {
		if (!$this->pre) return 0;
		$this->res = ($GLOBALS['DBDebug'] < 2) ? @mysql_query($this->pre) : 0;
		switch($this->kind) {
			case 'SELECT':
				if ($this->res) $this->num_rows = mysql_num_rows($this->res);
				break;

			case 'INSERT':
				if ($this->res) $this->insert_id = mysql_insert_id();
				break;

			case 'REPLACE':
				if ($this->res) $this->num_rows = $this->res;
				break;
				
			case 'UPDATE':
				if ($this->res) $this->num_rows = $this->res;
				break;
		}
	
		if ($GLOBALS['DBDebug']) print "<p>{$this->pre}<br>Result: {$this->res}</p>";		// ****************** debug output *****************
		return $this->res;
	}
	
	function fetch($key='') {
		$this->fet = array();
		if ($this->num_rows) {
			while ($r = mysql_fetch_assoc($this->res))
				if ($key)
					$this->fet[$r[$key]] = $r;
				else
					$this->fet[] = $r;
		}
		return $this->fet;
	}
	
	function result($field) {
		return mysql_result($this->res,0,$field);
	}
	
}